package config.table.xls;
import java.util.Arrays;

import game.tools.application.ApplicationParameters;
import game.tools.utils.StringTools;

public class Parameters 
{
	public static final String Mark = "\n"+
			"O       O    O         OOOO          OOOOOOOOO    OOO              OOOO          O O O         O           \n"+
			" O     O     O        O    OO            O      O      O          O    OO       O      O       O           \n"+
			"  O   O      O       O       O           O      O      O         O       O     O        O      O           \n"+
			"   O O       O        OO                 O      O      O          OO          O          O     O           \n"+
			"    O        O           OO              O      O      O             OO       O          O     O           \n"+
			"   O  O      O             OO            O      O      O               OO      O      O  O     O           \n"+
			"  O    O     O               O           O      O      O                 O      O       O      O           \n"+
			" O      O    O       O     OO            O      O      O         O     OO        O     O O     O           \n"+
			"O        O   OOOOOO   OOOOO              O        OOO             OOOOO           O O O    O   OOOOOO       \n\n";
	
	/** mysql数据库参数*/
	private String mysqlUrl , mysqlDatabase , mysqlUsername , mysqlPassword;
	/** 配置表目录 , 配置表忽略的后缀*/
	private String xlsDir, xlsIgnoreSuffix;
	/** 配置表指定表*/
	private String [] xlsTables;
	/** 数据开始列，数据开始行*/
	private int xlsDataStartColl , xlsDataStartRow;
	/** 数据表列名行，数据表列类型行 , 数据描述行*/
	private int xlsDataCollNameRow, xlsDataCollTypeRow , xlsDataCollDescRow;
	/** 处理完后通知地址，通知访问toekn*/
	private String serverNotifyUrl , serverAccessToken;
	
	private Parameters() {}
	
	public static final Parameters Instance = new Parameters();
	
	public void init(String [] args) throws Exception
	{
		if(isHelper(args))
			showHelp();
		
		mysqlUrl = ApplicationParameters.get(args, "mysql.url");
		mysqlDatabase = ApplicationParameters.get(args, "mysql.database");
		mysqlUsername = ApplicationParameters.get(args, "mysql.username");
		mysqlPassword = ApplicationParameters.get(args, "mysql.password");
		
		xlsDir = ApplicationParameters.get(args, "xls.dir");
		String xlsTablesString = ApplicationParameters.get(args, "xls.tables");
		if(!StringTools.empty(xlsTablesString))
			xlsTables = xlsTablesString.split(",");
		xlsIgnoreSuffix = ApplicationParameters.get(args, "xls.ignore.suffix");
		
		String xlsDataStartCollString = ApplicationParameters.get(args, "xls.data.start.coll");
		if(StringTools.empty(xlsDataStartCollString))
			throw new RuntimeException("xls.data.start.coll，value： "+xlsDataStartCollString+"，error !");
		xlsDataStartColl = Integer.parseInt(xlsDataStartCollString);
		
		String xlsDataStartRowString = ApplicationParameters.get(args, "xls.data.start.row");
		if(StringTools.empty(xlsDataStartRowString))
			throw new RuntimeException("xls.data.start.row，value： "+xlsDataStartRowString+"，error !");
		xlsDataStartRow = Integer.parseInt(xlsDataStartRowString);
		
		String xlsDataCollNameRowString = ApplicationParameters.get(args, "xls.data.coll.name.row");
		if(StringTools.empty(xlsDataCollNameRowString))
			throw new RuntimeException("xls.data.coll.name.row，value： "+xlsDataCollNameRowString+"，error !");
		xlsDataCollNameRow = Integer.parseInt(xlsDataCollNameRowString);
		
		String xlsDataCollTypeRowString = ApplicationParameters.get(args, "xls.data.coll.type.row");
		if(StringTools.empty(xlsDataCollTypeRowString))
			throw new RuntimeException("xls.data.coll.type.row，value： "+xlsDataCollTypeRowString+"，error !");
		xlsDataCollTypeRow = Integer.parseInt(xlsDataCollTypeRowString);
		
		String xlsDataCollDescRowString = ApplicationParameters.get(args, "xls.data.coll.desc.row");
		if(StringTools.empty(xlsDataCollDescRowString))
			throw new RuntimeException("xls.data.coll.desc.row，value： "+xlsDataCollDescRowString+"，error !");
		xlsDataCollDescRow = Integer.parseInt(xlsDataCollDescRowString);

		serverNotifyUrl =  ApplicationParameters.get(args, "server.notify.url");
		serverAccessToken =  ApplicationParameters.get(args, "server.access.token");
		showValue();
	}
	
	public void showValue()
	{
		System.out.printf(Mark);
		game.tools.utils.ConsoleTable consoleTable = new game.tools.utils.ConsoleTable("参数描述","参数名称", "运行值");
		consoleTable.setTitle("运行时参数一览");
		consoleTable.addRow("mysql-连接地址", 		"mysql.url",				mysqlUrl);
		consoleTable.addRow("mysql-数据库", 			"mysql.database",			mysqlDatabase);
		consoleTable.addRow("mysql-连接用户名", 		"mysql.username",			mysqlUsername);
		consoleTable.addRow("mysql-连接密码", 		"mysql.password",			mysqlPassword);
		consoleTable.addRow("配置表-目录地址", 		"xls.dir",					xlsDir);
		consoleTable.addRow("配置表-指定表名", 		"xls.tables",				Arrays.toString(xlsTables));
		consoleTable.addRow("配置表-忽略后缀", 		"xls.ignore.suffix",		xlsIgnoreSuffix);
		consoleTable.addRow("数据-起始列", 			"xls.data.start.coll",		xlsDataStartColl);
		consoleTable.addRow("数据-起始行", 			"xls.data.start.row",		xlsDataStartRow);
		consoleTable.addRow("数据-字段名称行",		"xls.data.coll.name.row",	xlsDataCollNameRow);
		consoleTable.addRow("数据-字段类型行",		"xls.data.coll.type.row",	xlsDataCollTypeRow);
		consoleTable.addRow("数据-字段描述行",		"xls.data.coll.desc.row",	xlsDataCollDescRow);
		consoleTable.addRow("通知-通知回调地址 ",		"server.notify.url",		serverNotifyUrl);
		consoleTable.addRow("通知-通知访问权限 ",		"server.access.token",		serverAccessToken);
		System.out.println(consoleTable);
	}
	
	private boolean isHelper(String [] args) 
	{
		if(args.length == 0)
			return false;
		return "-help".equalsIgnoreCase(args[0]) || "--help".equalsIgnoreCase(args[0]);
	}

	public void showHelp()
	{
		System.out.printf(Mark);
		game.tools.utils.ConsoleTable consoleTable = new game.tools.utils.ConsoleTable("参数描述","参数名称", "必填", "类型", "示例" , "备注");
		consoleTable.setTitle("参数列表");
		consoleTable.addRow("mysql-连接地址", 		"mysql.url",				"是",	"(String)字符串",	"例：mysql.url=127.0.0.1:3306。",									"地址:端口。");
		consoleTable.addRow("mysql-数据库", 			"mysql.database",			"是",	"(String)字符串",	"例：mysql.database=config-table。",									"");
		consoleTable.addRow("mysql-连接用户名", 		"mysql.username",			"是",	"(String)字符串",	"例：mysql.username=root。",											"");
		consoleTable.addRow("mysql-连接密码", 		"mysql.password",			"是",	"(String)字符串",	"例：mysql.password=root。",											"");
		consoleTable.addRow("配置表-目录地址", 		"xls.dir",					"是",	"(String)字符串",	"例：xls.dir=d:/table",												"");
		consoleTable.addRow("配置表-指定表名", 		"xls.tables",				"否",	"(String)字符串",	"例：xls.tables=RoleTable,MatchTable",								"只操作指定的表，多个用\",\"逗号隔开。");
		consoleTable.addRow("配置表-忽略后缀", 		"xls.ignore.suffix",		"否",	"(String)字符串",	"例：xls.ignore.suffix=Table",										"忽略的后缀，生成表名，不会应用到数据库中。");
		consoleTable.addRow("数据-起始列", 			"xls.data.start.coll",		"是",	"(Int)正数值",		"例：xls.data.start.coll=1",											"");
		consoleTable.addRow("数据-起始行", 			"xls.data.start.row",		"是",	"(Int)正数值",		"例：xls.data.start.row=1",											"");
		consoleTable.addRow("数据-字段名称行",		"xls.data.coll.name.row",	"是",	"(Int)正数值",		"例：xls.data.coll.name.row=3",										"");
		consoleTable.addRow("数据-字段类型行",		"xls.data.coll.type.row",	"是",	"(Int)正数值",		"例：xls.data.coll.type.row=2",										"");
		consoleTable.addRow("数据-字段描述行",		"xls.data.coll.desc.row",	"是",	"(String)字符串",	"例：xls.data.coll.desc.row=4",										"");
		consoleTable.addRow("通知-通知回调地址 ",		"server.notify.url",		"否",	"(String)字符串",	"例：server.notify.url=http://127.0.0.1:10001/configTable/update ",	"仅支持http请求方式。");
		consoleTable.addRow("通知-通知访问权限 ",		"server.access.token",		"否",	"(String)字符串",	"例：a384baf79c6c4bb78c34cd8c0a4d9879",								"当server.notify.url有配置时，token也必须指定，配套使用。");
		System.out.println(consoleTable);
		System.exit(-1);
	}

	public String getMysqlUrl() {		return mysqlUrl;	}
	public String getMysqlDatabase() {		return mysqlDatabase;	}
	public String getMysqlUsername() {		return mysqlUsername;	}
	public String getMysqlPassword() {		return mysqlPassword;	}
	public String getXlsDir() {		return xlsDir;	}
	public String[] getXlsTables() {		return xlsTables;	}
	public int getXlsDataStartColl() {		return xlsDataStartColl;	}
	public int getXlsDataStartRow() {		return xlsDataStartRow;	}
	public int getXlsDataCollNameRow() {		return xlsDataCollNameRow;	}
	public int getXlsDataCollTypeRow() {		return xlsDataCollTypeRow;	}
	public String getServerNotifyUrl() {		return serverNotifyUrl;	}
	public String getXlsIgnoreSuffix() {		return xlsIgnoreSuffix;	}
	public int getXlsDataCollDescRow() {		return xlsDataCollDescRow;	}
	public String getServerAccessToken() {		return serverAccessToken;	}
}